<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>类别列表</el-breadcrumb-item>
    </el-breadcrumb>


    <el-divider></el-divider>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center">
      </el-table-column>
      <el-table-column prop="name" label="类别名" width="130" align="center">
      </el-table-column>
      <el-table-column prop="parentId" label="父级类别id" width="80" align="center">
      </el-table-column>
      <el-table-column prop="depth" label="深度" width="80" align="center">
      </el-table-column>
      <el-table-column prop="keywords" label="关键词"  header-align="center">
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="100" align="center">
      </el-table-column>
      <el-table-column prop="isParent" label="是否父级" width="100" align="center">
      </el-table-column>
      <el-table-column label="显示在导航栏" width="120" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="handleChangeDisplay(scope.row)"
              v-model="scope.row.isDisplay"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用" width="65" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.id == 1"
              active-color="#13ce66"
              inactive-color="#aaa">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)"
                     icon="el-icon-edit" circle size="mini"></el-button>
          <el-button type="danger" @click="openDeleteConfrim(scope.row)"
                     icon="el-icon-delete" circle size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>


<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      displayText: ['不显示在导航栏', '显示在导航栏'],
    }
  },
  methods:{
    handleChangeDisplay(category) {
      let url = 'http://localhost:9080/categories/' + category.id;
      if (category.isDisplay == 1) {
        url += '/display';
      } else {
        url += '/hidden';
      }
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '操作成功，已经将【' + category.name + '】的状态改为【' + this.displayText[category.isDisplay] + '】！',
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        this.loadCategoryList();
      });
    },
    changeEnable(category){
      let enableText = ['禁用', '启用'];
      let url = "http://127.0.0.1:9080/categories/"+category.id;
      if (category.enable == 1){
        url += "/enable";
        console.log(url)

      }else {
        url += "/disable";
        console.log(url);
      }
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = '将相册【' + category.name + '】的启用状态改为【' + enableText[category.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 40400) {
          this.loadCategoryList();
        }
      });
    },



    handleEdit(category){
      let message = "您正在尝试编辑["+category.name+"]的详情,抱歉,该功能待完善"
      this.$alert(message, '提示', {
        confirmButtonText: '确定',
      });
    },
    handleDelete(category) {
      console.log('准备删除分类……id = '+category.id);
      let url = "http://127.0.0.1:9080/categories/"+category.id+"/delete";
      console.log("url = "+url);
      axios.post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 20000){
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        }
        if (responseBody.state == 20000 || responseBody.state == 40400) {
          this.loadCategoryList();
        }
      })
    },
    openDeleteConfrim(category) {
      this.$confirm('此操作将永久删除['+category.name +']分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {
      });
    },
    loadCategoryList() {
      console.log("loadCategoryList")
      let url = "http://127.0.0.1:9080/categories";
      axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      })
    }
  },
  mounted(){
    this.loadCategoryList();
  }
}
</script>
